In [1]:
from CoolProp.CoolProp import Props
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
%matplotlib inline
Each ancillary equation can be decomposed into a form like $$ L = \sum_{i} n_i\Theta^{t_i} $$ where $\Theta = 1-\dfrac{T}{T_c}$ and the left hand side $L$ depends on the form to be fit.
Four forms are commonly used depending on the curve being fit
Saturated liquid: $$ L = \dfrac{\rho'}{\rho_c}-1 $$
Saturated vapor: $$ L = \log\left(\dfrac{\rho''}{\rho_c}\right)\dfrac{T}{T_c} $$
Saturation pressure: $$ L = \log\left(\dfrac{p''}{p_c}\right)\dfrac{T}{T_c} $$
The left hand side and right hand side can be evaluated for a large number of "experimental" data points obtained from the EOS.
The best fit coefficients $n_i$ will yield the ancillary values
$$\sum_m L_m = \sum_m\left[\sum_{i=0}^{I-1} n_i\Theta_m^{t_i}\right]$$The residual is then given by
$$ \zeta = a_{0}- \sum_{i=0}^{I-1}n_ia_{i}$$where $a_0$ is $L$, and $a_i$ is $\Theta^{t_i}$
The sum of the squares of the residuals over the $M$ "measured" data points is given by
$$ \chi^2 = \sum_{m=0}^{M-1}\left(a_{0,m}- \sum_{i=0}^{I-1}n_ia_{i,m}\right)^2\cdot \frac{1}{\sigma_m}$$We then build a $\mathbf{A}$ matrix and $\mathbf{b}$ vector, following the methods of Span (2000, p. 64) where the entries are
$$ a_{ij} = \sum_{m=0}^{M-1}\left(\Theta_m^{t_i}\Theta_m^{t_j} \right) $$and
$$ b_{i} = \sum_{m=0}^{M-1}\left(\Theta_m^{t_i}L_m \right) $$And then solve the matrix equation
$$\mathbf{A}\mathbf{n} = \mathbf{b} $$to get $\mathbf{n}$
In [ ]: